package generate;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class FastGenerateMain {

    public static final String configProperties = "gen.properties";

    public static void main(String[] args) {

        ClassLoader classLoader = FastGenerateMain.class.getClassLoader();
        InputStream is = classLoader.getResourceAsStream(configProperties);
        Properties properties = new Properties();
        try {
            properties.load(is);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        String url = properties.getProperty("spring.datasource.url");
        String username = properties.getProperty("spring.datasource.username");
        String password = properties.getProperty("spring.datasource.password");

        File file = new File(classLoader.getResource(configProperties).getPath());
        // 输出到 target/generate 目录
        String output = file.getParentFile().getParent() + "/generate";
        System.out.println(output);
        // 参考 https://blog.csdn.net/abc123adc/article/details/130205285
        FastAutoGenerator.create(url, username, password)
                .globalConfig(builder -> {
                    builder.disableOpenDir()
                            .outputDir(output)
                            .author("aoliao")
                            .dateType(DateType.TIME_PACK)
                            .commentDate("yyyy-MM-dd");
                })
                .packageConfig(builder -> {
                    builder.parent("com.ruoyi.tj")
                            .entity("entity")
                            .service("service")
                            .serviceImpl("service.impl")
                            .mapper("mapper")
                            .xml("mapping");
                })
                .templateConfig(builder -> {

                })
                .strategyConfig(builder -> {
                    builder.enableCapitalMode()
                                    .addTablePrefix("tj_");
                    builder.entityBuilder()
                            .enableFileOverride()
                            .enableChainModel()
                            .enableTableFieldAnnotation()
                            .columnNaming(NamingStrategy.underline_to_camel)
                            .idType(IdType.ASSIGN_ID);
                    builder.controllerBuilder()
                            .enableFileOverride()
                            .enableHyphenStyle()
                            .enableRestStyle()
                            .formatFileName("%sController");
                    builder.serviceBuilder()
                            .enableFileOverride()
                            .superServiceClass(IService.class)
                            .superServiceImplClass(ServiceImpl.class)
                            .formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImpl");
                    builder.mapperBuilder()
                            .enableFileOverride()
                            .superClass(BaseMapper.class)
                            .enableMapperAnnotation()
                            .enableBaseResultMap()
                            .enableBaseColumnList()
                            .formatMapperFileName("%sMapper")
                            .formatXmlFileName("%sMapper");
                })
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }
}
